package com.example.medicalretrieval;

import android.content.Intent;
import android.os.Bundle;

import androidx.constraintlayout.utils.widget.ImageFilterButton;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.example.medicalretrieval.adapter.PDFItemAdapter;
import com.example.medicalretrieval.pojo.PdfItem;
import com.example.medicalretrieval.pojo.PdfRestInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class SearchFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {


    private EditText etSearch;
    private View view;
    private List<PdfItem> pdfItemList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_search, container, false);

        etSearch = view.findViewById(R.id.et_search);
        ImageFilterButton searchButton = view.findViewById(R.id.btn_search);
        searchButton.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_search:
                String searchContext = etSearch.getText().toString();
                getSearchResult(searchContext);
                break;
        }
    }

    private void getSearchResult(String searchContext) {

        //   http://localhost:8080/item/findByTitle?title=左心房&authors&content&current
        new Thread(()->{
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://192.168.43.144:8080/item/find?title="+etSearch.getText().toString())
                    .get()
                    .build();

            Response response = null;
            try {
                response = client.newCall(request).execute();
                String data = response.body().string();
                System.out.println("data数据");
                System.out.println(data);
                Type type = new TypeToken<List<PdfRestInfo>>() {}.getType();
                List<PdfRestInfo> pdfRestInfoList = new Gson().fromJson(data,type);
//                Type page4NavigatorType = new TypeToken<Page4Navigator<ReturnDoc>>(){}.getType();
//                Page4Navigator<ReturnDoc> page4Navigator = new Gson().fromJson(data, page4NavigatorType);
//
//                showData(page4Navigator);
                showData(pdfRestInfoList);
            } catch (IOException e) {
                e.printStackTrace();
            }



        }).start();
    }

    private void showData(List<PdfRestInfo> pdfRestInfoList){
        requireActivity().runOnUiThread(() -> {

            pdfItemList = new ArrayList<>();
            for (PdfRestInfo p :
                    pdfRestInfoList) {
                PdfItem pdfItem = new PdfItem();
                pdfItem.setTitle(p.getName());
                pdfItem.setAuthors(p.getAuthors());
                pdfItem.setAbstractContent(p.getTextSummary());
                pdfItem.setFilePath(p.getFilePath());
                pdfItemList.add(pdfItem);
            }
//            System.out.println("pdf数据接收成功");
            PDFItemAdapter adapter = new PDFItemAdapter(getContext(), pdfItemList);
            ListView listView = view.findViewById(R.id.search_item);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(this);

        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(),ShowPdfActivity.class);
        Bundle bundle = new Bundle();
        PdfItem pdfItem = pdfItemList.get(position);
        bundle.putString("title",pdfItem.getTitle());
        bundle.putString("filePath",pdfItem.getFilePath());
        bundle.putString("name",pdfItem.getName());
        bundle.putString("authors",pdfItem.getAuthors());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}